package com.btr.ivle.helper;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseModule extends SQLiteOpenHelper {

	//private static final int DATABASE_VERSION = 1;
	
	//static final String dbName="ivleDB";

	private String tableName=null;
	public final String colID="CourseID";
	public final String colCode="CourseCode";
	public final String colName="CourseName";
	public final String colAY="CourseAcadYear";
	public final String colSem="CourseSemester";
	public final String colNewAnnounce="BadgeAnnouncement";
	public final String colCourseMC="CourseMC";
	//public final String colLecturers="Lecturers";  must have
	//public final String colDescriptions="Descriptions"; must have

	//public final String colAnnounce="Announcements"; must have
	public final String colForums="Forums"; // no.
	public final String colWorkbins="Workbins"; // no.
	public final String colWebcasts="Webcasts"; // no.
	public final String colGradebooks="Gradebooks"; //no.
	public final String colWebLinks="WebLinks"; // no.

	static final String viewEmps="ViewEmps";
	
	public DatabaseModule(Context context, String name, CursorFactory factory, int version) {
		super(context, name, factory, version);
		this.tableName = name;
	}
	
	public void onCreate(SQLiteDatabase db) {
		  db.execSQL("CREATE TABLE IF NOT EXISTS "+ tableName +" ( "+ colCode + " TEXT PRIMARY KEY , "+
				  colName + " TEXT, " + colAY + " TEXT, " + colSem + " TEXT, " + colID + " TEXT, " + 
				  colNewAnnounce + " INTEGER, " + colCourseMC + " TEXT, " + colForums + " INTEGER, " + 
				  colWorkbins + " INTEGER, " + colWebcasts + " INTEGER, " + colGradebooks + " INTEGER, "
				  + colWebLinks + " INTEGER)"
				  );
	}
	
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		  db.execSQL("DROP TABLE IF EXISTS "+ tableName);
		  onCreate(db);
		 }
	
	/*void addModule(ModulesEntity Module) {
        SQLiteDatabase db = this.getWritableDatabase();
 
        ContentValues values = new ContentValues();
        values.put(colCode, Module.getCourseCode());
        values.put(colName, Module.getCourseName());
        values.put(colAY, Module.getCourseAcadYear());
        values.put(colSem, Module.getCourseSemester());
 
        db.insert(colCode, null, values);
        db.close(); 
    }
 
	ModulesEntity getModule(String code) {
        SQLiteDatabase db = this.getReadableDatabase();
 
        Cursor cursor = db.query(tableName, new String[] { colCode,
        		colName, colAY, colSem }, colCode + "=?",
                new String[] { code }, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();
 
        ModulesEntity Module = new ModulesEntity(cursor.getString(0),
                cursor.getString(1), cursor.getString(2), cursor.getString(3));
        return Module;
    }
	
	public List<ModulesEntity> getAllModules() {
        List<ModulesEntity> ModulesList = new ArrayList<ModulesEntity>();
        String selectQuery = "SELECT  * FROM " + tableName;
 
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
 
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
            	ModulesEntity Module = new ModulesEntity();
            	Module.setCourseCode(cursor.getString(0));
            	Module.setCourseName(cursor.getString(1));
            	Module.setCourseAcadYear(cursor.getString(2));
            	Module.setCourseSemester(cursor.getString(3));
                // Adding contact to list
            	ModulesList.add(Module);
            } while (cursor.moveToNext());
        }

        return ModulesList;
    }
	
	public int updateContact(ModulesEntity Module) {
        SQLiteDatabase db = this.getWritableDatabase();
 
        ContentValues values = new ContentValues();
        values.put(colCode, Module.getCourseCode());
        values.put(colName, Module.getCourseName());
        values.put(colAY, Module.getCourseAcadYear());
        values.put(colSem, Module.getCourseSemester());

        return db.update(tableName, values, colCode + "=?",
                new String[] { Module.getCourseCode() });
    }*/
}
